import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
//import QtQuick.Controls.Styles

import Shared

/*基础上GamePage.qml，所以有errorMessage,infoMessage。
  有1个title，1个ListView和2个GameButton。
*/
GamePage {
    /*QQ会重载吗？
    */
    function init()
    {
        console.log("show ParaSet.qml")
        row_info.vis= false;
    }


    Text {
        id: txt_title
        anchors.top: parent.top


        text: qsTr("通用参数")
        color: "green"
        anchors.horizontalCenter: parent.horizontalCenter

        font.pointSize: 48
    }


    //上电自动清零
    RowLayout{
        id: row1
        anchors.top: txt_title.bottom

        Text {
            text: qsTr("上电自动清零")
            color: "green"
        }

        ComboBox{
            id: combobox_min
            model: ["off", "on"]

            currentIndex: deviceHandler.zero_auto_sw_idx
        }

        Button{
            id: btn_pwr_zero_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read pwr_zero");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_zero_auto_sw_idx, 0);
            }
        }

        Button{
            id: btn_pwr_zero_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write pwr_zero");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_zero_auto_sw_idx, combobox_min.currentIndex);
            }
        }
    }


    //零点跟踪范围
    RowLayout{
        id: row2
        anchors.top: row1.bottom

        Text {
            text: qsTr("零点跟踪范围")
            color: "green"
        }

//        TextField{

//            text: "0"
//        }
        ComboBox{
            id: combobox_max
            model: ["0", "1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d"]

            currentIndex: deviceHandler.range_zero_trace_idx
        }

        Button{
            id: btn_range_zero_trance_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read range_zero_trance");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_range_zero_trace_idx, 0);
            }
        }

        Button{
            id: btn_range_zero_trance_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write range_zero_trance");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_range_zero_trace_idx, combobox_max.currentIndex);
            }
        }
    }


    //判稳范围
    RowLayout{
        id: row3
        anchors.top: row2.bottom

        Text {
            text: qsTr("判稳范围　　")
            color: "green"
        }

//        TextField{

//            text: "0"
//        }
        ComboBox{
            id: combobox_range_stab
            model: ["1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d"]

            currentIndex: deviceHandler.range_stab_idx
        }

        Button{
            id: btn_range_stab_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read range_stab");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_range_stab_idx, 0);
            }
        }

        Button{
            id: btn_range_stab_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write range_stab");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_range_stab_idx, combobox_range_stab.currentIndex);
            }
        }
    }


    //清零范围
    RowLayout{
        id: row4
        anchors.top: row3.bottom

        Text {
            text: qsTr("清零范围　　")
            color: "green"
        }

        TextField{
            id: txtfd_range_zero
            text: deviceHandler.range_zero      //QQ可以直接用int赋值。
            color: "white"

            inputMask: "00"
        }


        Button{
            id: btn_range_zero_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read range_zero");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_range_zero, 0);
            }
        }

        Button{
            id: btn_range_zero_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write range_zero");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_range_zero, Number(txtfd_range_zero.text));
            }
        }
    }


    //数字滤波参数
    RowLayout{
        id: row5
        anchors.top: row4.bottom

        Text {
            text: qsTr("数字滤波参数")
            color: "green"
        }


        ComboBox{
            id: combobox_filter_dig
            model: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

            currentIndex: deviceHandler.filter_dig_idx
        }

        Button{
            id: btn_filter_dig_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read filter_dig");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_filter_dig_idx, 0);
            }
        }

        Button{
            id: btn_filter_dig_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write filter_dig");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_filter_dig_idx, combobox_filter_dig.currentIndex);
            }
        }
    }


    //稳态滤波参数
    RowLayout{
        id: row6
        anchors.top: row5.bottom

        Text {
            text: qsTr("稳态滤波参数")
            color: "green"
        }


        ComboBox{
            id: combobox_filter_stab
            model: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

            currentIndex: deviceHandler.filter_stab_idx
        }

        Button{
            id: btn_filter_stab_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read filter_stab");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_filter_stab_idx, 0);
            }
        }

        Button{
            id: btn_filter_stab_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write filter_stab");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_filter_stab_idx, combobox_filter_stab.currentIndex);
            }
        }
    }


    //ＡＣ采样频率
    RowLayout{
        id: row_sps
        anchors.top: row6.bottom

        Text {
            text: qsTr("ＡＤ采样频率")
            color: "green"
        }


        ComboBox{
            id: combobox_ad_sps
            model: ["120", "480", "960"]

            currentIndex: deviceHandler.sps_idx
        }

        Button{
            id: btn_ad_sps_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read ad sps");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_sps_idx, 0);
            }
        }

        Button{
            id: btn_ad_sps_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write ad sps");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_sps_idx, combobox_ad_sps.currentIndex);
            }
        }
    }

    RowLayout{
        id: row_in1
        anchors.top: row_sps.bottom
        anchors.topMargin: 10

        Text {
            text: qsTr("   IN1定义")
            color: "green"
        }

        ComboBox{
            id: combobox_in1
            model: ["0", "I1", "I2", "I3", "I4", "I5", "I6", "I7", "I8/LAJI"]

            currentIndex: deviceHandler.in1_define_idx
        }


        Button{
            id: btn_in1_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read in1 define");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_in1_define_idx, 0);
            }
        }

        Button{
            id: btn_in1_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write in1 define");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_in1_define_idx, combobox_in1.currentIndex);
            }
        }
    }


    RowLayout{
        id: row_out1
        anchors.top: row_in1.bottom

        Text {
            text: qsTr("OUT1定义")
            color: "green"
        }

        ComboBox{
            id: combobox_out1
            model: ["0/NO", "o1", "o2", "o3", "o4", "o5", "o6"]

            currentIndex: deviceHandler.out1_define_idx
        }


        Button{
            id: btn_out1_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read out1 define");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_out1_define_idx, 0);
            }
        }

        Button{
            id: btn_out1_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write out1 define");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_out1_define_idx, combobox_out1.currentIndex);
            }
        }
    }


    RowLayout{
        id: row_out2
        anchors.top: row_out1.bottom

        Text {
            text: qsTr("OUT2定义")
            color: "green"
        }

        ComboBox{
            id: combobox_out2
            model: ["0/NO", "o1", "o2", "o3", "o4", "o5", "o6"]

            currentIndex: deviceHandler.out2_define_idx
        }


        Button{
            id: btn_out2_rd
            text: qsTr("读取")
            //color: pressed?red:green

            onClicked: {
                console.log("read out2 define");
                row_info.vis= true;
                deviceHandler.data03h_to_periph(GameSettings.addr_out2_define_idx, 0);
            }
        }

        Button{
            id: btn_out2_wr
            text: qsTr("写入")
            //color: pressed?red:green

            onClicked: {
                console.log("write out2 define");
                row_info.vis= true;
                deviceHandler.data10h_to_periph(GameSettings.addr_out2_define_idx, combobox_out2.currentIndex);
            }
        }
    }


    //提示信息。
    RowLayout{
        property bool vis: true

        id: row_info
        anchors.bottom: txt_help.top
        anchors.horizontalCenter: parent.horizontalCenter

        //Layout.alignment: Qt.AlignHCenter

        Text{
            text:qsTr(" ")      //占位，以免下面的Text{}隐藏时，造成下面的控件位置变化。
        }

        Text{
            id: txt_info_set
            anchors.centerIn: parent.Center
            visible: deviceHandler.txt_ok_v && parent.vis

            //text: qsTr("成功")
            text: deviceHandler.txt_info
            color: deviceHandler.txt_color   //"green"
        }
    }


    //帮助信息
    Text {
        id: txt_help
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter

        text: qsTr("须先依次点击读取，以更新界面")
        color: "green"


        font.pointSize: 24
    }



    Timer{
        id: timer_info_set
        interval: 500
        running: deviceHandler.txt_ok_v && row_info.vis

        onTriggered: {
            console.log("timer tmo")
            row_info.vis= false
            //row_info.txt_info_set.visible= false;   //和绑定属性deviceHandler.txt_ok_v不同步了。
        }
    }
}
